/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTE:
 * Derivative work based on a sample project from CommonsWare, LLC
 * From _The Busy Coder's Guide to Advanced Android Development_
 *		http://commonsware.com/AdvAndroid
 */

package edu.bsu.android.gtv.apps.bconnected.ui;

import edu.bsu.android.gtv.apps.bconnected.R;
import edu.bsu.android.gtv.apps.bconnected.objects.Organization;
import edu.bsu.android.gtv.apps.bconnected.ui.base.BaseActivity;
import edu.bsu.android.gtv.apps.bconnected.util.Constants;
import edu.bsu.android.gtv.apps.bconnected.util.UIUtils;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;

/**
 * A multi-pane activity, consisting of a {@link CategoryFragment} and a {@link OrganizationFragment} 
 * @author Kyle Parker, University Computing Services
 */
public class OrganizationMultiPaneActivity extends BaseActivity implements 
	CategoryFragment.OnCategoryListener,
	OrganizationFragment.OnOrganizationListener { 

    private FragmentManager mFragmentManager;
    private CategoryFragment mCategoryFragment;
	private Activity mActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_organization);

		mActivity = this;
        getActivityHelper().setupActionBar(false);
        
        mFragmentManager = getFragmentManager();
        mCategoryFragment = (CategoryFragment) mFragmentManager.findFragmentById(R.id.fragment_category_list); 
        mCategoryFragment.setOnCategoryListener(this);
        mCategoryFragment.enablePersistentSelection();

        UIUtils.setTitle(mActivity, "Student Orgs");
    }

    /**
     * Create a fragment transaction and load the organizations for the selected category
     * loadOrganizations found in {@link OrganizationFragment}
     * @param position
     */
	public void addOrganizationFragment(int position) {
		OrganizationFragment orgs = (OrganizationFragment) mFragmentManager.findFragmentById(R.id.fragment_container_organization_list);
		FragmentTransaction ft = mFragmentManager.beginTransaction();
		
		if (orgs == null) {
			orgs = new OrganizationFragment(true);
			orgs.setOnOrganizationListener(this);
			
			ft.add(R.id.fragment_container_organization_list, orgs)
				.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
				.addToBackStack(null)
				.commit();
		}
			
		orgs.startActivity(position);
	}
	
	/**
	 * @see edu.bsu.android.apps.gtv.mybsu.ui.CategoryFragment.OnCategoryListener#onCategorySelected(int)
	 * When the user taps a category, the OnCategoryListener in the CategoryFragment will fire, calling this method
	 * The addOrganizationFragment method will be called, creating a fragment transaction and loading the organizations based on the category selected
	 */
	public void onCategorySelected(int position) { 
		addOrganizationFragment(position);
	}
	
	/**
	 * @see edu.bsu.android.apps.gtv.mybsu.ui.OrganizationFragment.OnOrganizationListener#onOrganizationSelected(edu.bsu.android.apps.gtv.mybsu.objects.Organization)
	 * When an organization is selected, start a new fragment to display the organization information
	 */
	public void onOrganizationSelected(Organization org, int categoryPosition) {
		Intent intent = new Intent(mActivity, OrganizationInfoActivity.class);
        intent.putExtra(Constants.EXTRA_CATEGORY_POSITION, Integer.toString(categoryPosition));
        intent.putExtra(Constants.EXTRA_ORGANIZATION_ID, org.getOrganizationID());
		
		mActivity.startActivity(intent);
	}
}
